package com.etrans.web.gps.business.gis.controller;

import java.util.ArrayList;
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.etrans.egov.base.intf.ObjDriver;
import com.etrans.web.gps.base.controller.BaseController;
import com.etrans.web.gps.business.gis.domain.Track;
import com.etrans.web.gps.business.gis.domain.TrackExportVo;
import com.etrans.web.gps.business.gis.service.TrackPlayBackService;
import com.etrans.web.gps.business.system.domain.SysUser;
import com.etrans.web.gps.business.system.service.CommonService;
import com.etrans.web.gps.common.page.PageVo;
import com.etrans.web.gps.util.Config;
import com.etrans.web.gps.util.DateUtil;

/**
 * <p>轨迹回放</p>
 */
@Controller
@RequestMapping("/gis/playback")
public class TrackPlayBackController extends BaseController{
	
    @Autowired
    private TrackPlayBackService trackPlayBackService;
    
	@Autowired
	private CommonService commonService;
	
	@Autowired  
    private Config config; //引用统一的参数配置类  
	
    @RequestMapping("trackPlayBack.html")
    public ModelAndView trackPlayBack(String vehicleId,String starttime,String endtime){
    	ModelAndView view = new ModelAndView();
    	view.addObject("vehicleId", vehicleId);
    	view.addObject("startTime", starttime == null || "".equals(starttime) ? DateUtil.getDayStart(new Date()) : starttime);
    	view.addObject("endTime", endtime == null || "".equals(endtime) ? DateUtil.getDateTimeString(new Date(),null) : endtime);
    	
    	view.setViewName("/views/business/gis/trackPlayBack");
    	return view;
    }
    
    @RequestMapping("loadUserVehicle.html")
    public void loadUserVehicle(HttpServletResponse response,HttpServletRequest request){
    	String name = request.getParameter("name");
    	SysUser user = getSysUser();
    	HashMap<String,Object> param = new HashMap<String, Object>();
		param.put("fullID", user.getFullID());
		param.put("selected", "selected");
		param.put("vehicle_no", name);
		if(name != null && name.contains("_")){
			param.put("vehicle_no", name.split("_")[0]);
			param.put("vehicle_color", name.split("_")[1]);
		}
    	
    	List<HashMap<String,Object>> list = commonService.queryVeListByRole(param);
    	this.ajaxResultJson(response, list);
    }
    
   
    @RequestMapping("queryTrackList.html")
    @ResponseBody
    public PageVo queryTrack(String vehicle_no_color,String start_time,String end_time) throws Exception{
    	String vehicle_no = new String(Base64.getDecoder().decode(vehicle_no_color),"utf-8");
    	PageVo pageVo = new PageVo();
    	
    	List<Track> hisTrack = trackPlayBackService.getHisTrack(vehicle_no,start_time,end_time);
    	
    	pageVo.setPage(1);
    	pageVo.setTotal(hisTrack.size());
    	pageVo.setRows(hisTrack);
        return  pageVo;
    }
    
    @RequestMapping("queryDriverList.html")
    @ResponseBody
    public String queryDriverList(HttpServletResponse response,String vehicle_no_color,String start_time,String end_time) throws Exception{
    	String vehicle_no = new String(Base64.getDecoder().decode(vehicle_no_color),"utf-8");
    	
    	List<ObjDriver> hisDriver = trackPlayBackService.getHisDriver(vehicle_no,start_time,end_time);
    	for (ObjDriver objDriver : hisDriver) {
    		if(objDriver != null){
        		HashMap<String, Object> param = new HashMap<String, Object>();
        		param.put("licence", objDriver.getLicence());
        		HashMap<String, Object> driver = trackPlayBackService.getDriver(param);
        		if(driver != null){
        			objDriver.setImgUrl(config.imagePath + String.valueOf(driver.get("img_url")));
        		}else{
        			objDriver.setImgUrl("");
        		}
        	}
		}
    	
    	this.ajaxResultJson(response, hisDriver);
    	return null;
    }
    
    
  
    /**
   	 * 导出报表
   	 * @return
   	 */
   	@SuppressWarnings("unchecked")
	@RequestMapping(value="exportTrackBackInfo.html")
   	public void exportTrackBackInfo(HttpServletResponse response,HttpServletRequest request,TrackExportVo param ){
   		List<HashMap<String, Object>> colModelList = new ArrayList<HashMap<String, Object>>();
   		List<String> imgsrcList   = null;//JSON.parseObject(param.getImgsrc(), List.class);
   		List<String> rowsNameList = new ArrayList<String>(); 
   		List rowsList = new ArrayList<>();
   		if(!"".equals(param.getObjTableDataStr())){
   			HashMap<String, Object> tempHash=new HashMap<String, Object>();
   			String titleArray =param.getObjTableDataTtiles();
   			String columnArray ="a,b,c,d,e,f,g,h,i";
   			String widthArray ="120,120,120,120,120,120,120,120,120";
   			String[] columnArrSpt=columnArray.split(",");
   			tempHash.put("titleArray", titleArray.split(","));
   			tempHash.put("columnArray",columnArrSpt );
   			tempHash.put("widthArray", widthArray.split(","));
   			colModelList.add(tempHash);
   			rowsNameList.add(param.getObjTableDataName());
   			String[] tepmrows = param.getObjTableDataStr().split("\\&");
   	   		List<HashMap<String, String>> rows = new ArrayList<HashMap<String, String>>();
   			List<HashMap<String,Object>> tempList=new ArrayList<HashMap<String,Object>>();
   			for (String track : tepmrows) {
   				try
   				{
   					HashMap<String,Object> tpHash=new HashMap<String,Object>();
   					String[] trackArray = track.split("\\#");
   					for(int i=0;i<columnArrSpt.length;i++){
   						if("undefined".equals(trackArray[i])){
   							tpHash.put(columnArrSpt[i], "");
   						}else{
   							tpHash.put(columnArrSpt[i], trackArray[i]);
   						}
   						
   					}
   					tempList.add(tpHash);
   				
   				} catch (Exception e){
   					e.printStackTrace();
   				}
   				
   			}
   			rowsList.add(tempList);
   		}
   		
   		exportExls(request, response,param.getTitle(), colModelList,rowsList,imgsrcList,rowsNameList);
   	}
   		
    /**
	 * 导出报表
	 * @return
	 */
   	/*	@RequestMapping(value="exportTrackBackInfoOps.jhtml")
	@ResponseBody
	public void exportTrackBackInfoOps(HttpServletResponse response,HttpServletRequest request,TrackExportVo param ){
		List<HashMap<String, Object>> colModelList = new ArrayList<HashMap<String, Object>>();
		List<String> imgsrcList   = null;//JSON.parseObject(param.getImgsrc(), List.class);
		List rowsList = new ArrayList<>();
		List<HashMap<String, String>> rows = new ArrayList<HashMap<String, String>>();
		List<String> rowsNameList = new ArrayList<String>(); 
		if(!"".equals(param.getObjTableDataStr())){
			HashMap<String, Object> tempHash=new HashMap<String, Object>();
			String titleArray =param.getObjTableDataTtiles();
			String columnArray ="a,b,c,d,e,f,g,h,i";
			String widthArray ="120,120,120,120,120,120,120,120,120";
			String[] columnArrSpt=columnArray.split(",");
			tempHash.put("titleArray", titleArray.split(","));
			tempHash.put("columnArray",columnArrSpt );
			tempHash.put("widthArray", widthArray.split(","));
			colModelList.add(tempHash);
			rowsNameList.add(param.getObjTableDataName());
			String[] tepmrows = param.getObjTableDataStr().split("\\&");
			List<HashMap<String,Object>> tempList=new ArrayList<HashMap<String,Object>>();
			for (String track : tepmrows) {
				try
				{
					HashMap<String,Object> tpHash=new HashMap<String,Object>();
					String[] trackArray = track.split("\\#");
					for(int i=0;i<columnArrSpt.length;i++){
						if("undefined".equals(trackArray[i])){
							tpHash.put(columnArrSpt[i], "");
						}else{
							tpHash.put(columnArrSpt[i], trackArray[i]);
						}
						
					}
					tempList.add(tpHash);
				
				} catch (Exception e){
					e.printStackTrace();
				}
				
			}
			rowsList.add(tempList);
		}
		
		
		if(!"".equals(param.getMonitorListDataStr())){
			HashMap<String, Object> tempHash=new HashMap<String, Object>();
			String titleArray =param.getMonitorListTtiles();
			String columnArray ="a,b,c,d,e,f,g,h,i,";
			String widthArray ="120,120,120,120,120,120,120,120,120,";
			String[] columnArrSpt=columnArray.split(",");
			tempHash.put("titleArray", titleArray.split(","));
			tempHash.put("columnArray",columnArrSpt );
			tempHash.put("widthArray", widthArray.split(","));
			colModelList.add(tempHash);
			rowsNameList.add(param.getMonitorListName());
			String[] tepmrows = param.getMonitorListDataStr().split("\\&");
			List<HashMap<String,Object>> tempList=new ArrayList<HashMap<String,Object>>();
			for (String track : tepmrows) {
				try
				{
					HashMap<String,Object> tpHash=new HashMap<String,Object>();
					String[] trackArray = track.split("\\#");
					for(int i=0;i<columnArrSpt.length;i++){
						if("undefined".equals(trackArray[i])){
							tpHash.put(columnArrSpt[i], "");
						}else{
							tpHash.put(columnArrSpt[i], trackArray[i]);
						}
						
					}
					tempList.add(tpHash);
				
				} catch (Exception e){
					e.printStackTrace();
				}
				
			}
			rowsList.add(tempList);
		}
		
		if(!"".equals(param.getAlarmDetailListDataStr())&&!"".equals(param.getExportAlarmColumns())){
			HashMap<String, Object> tempHash=new HashMap<String, Object>();
			String titleArray =param.getAlarmDetailListTtiles();
			String columnArray =param.getExportAlarmColumns();
			
			String widthArray ="";
			String[] columnArrSpt=columnArray.split(",");
			for(int i=0;i<columnArrSpt.length;i++){
				if(i==0){
					widthArray=widthArray+"120";
				}else{
					widthArray=widthArray+",120";
				}
			}
			tempHash.put("titleArray", titleArray.split(","));
			tempHash.put("columnArray",columnArrSpt );
			tempHash.put("widthArray", widthArray.split(","));
			colModelList.add(tempHash);
			rowsNameList.add(param.getAlarmDetailListName());
			String[] tepmrows = param.getAlarmDetailListDataStr().split("\\&");
			List<HashMap<String,Object>> tempList=new ArrayList<HashMap<String,Object>>();
			for (String track : tepmrows) {
				try
				{
					HashMap<String,Object> tpHash=new HashMap<String,Object>();
					String[] trackArray = track.split("\\#");
					for(int i=0;i<columnArrSpt.length;i++){
						if("undefined".equals(trackArray[i])){
							tpHash.put(columnArrSpt[i], "");
						}else{
							tpHash.put(columnArrSpt[i], trackArray[i]);
						}
						
					}
					tempList.add(tpHash);
				
				} catch (Exception e){
					e.printStackTrace();
				}
				
			}
			rowsList.add(tempList);
		}

		if(!"".equals(param.getAlarmDetailList2DataStr())){

			HashMap<String, Object> tempHash=new HashMap<String, Object>();
			String titleArray =param.getAlarmDetailList2Ttiles();
			String columnArray =param.getExportAlarmColumns();
			
			String widthArray ="";
			String[] columnArrSpt=columnArray.split(",");
			for(int i=0;i<columnArrSpt.length;i++){
				if(i==0){
					widthArray=widthArray+"120";
				}else{
					widthArray=widthArray+",120";
				}
			}
			tempHash.put("titleArray", titleArray.split(","));
			tempHash.put("columnArray",columnArrSpt );
			tempHash.put("widthArray", widthArray.split(","));
			colModelList.add(tempHash);
			rowsNameList.add(param.getAlarmDetailList2Name());
			String[] tepmrows = param.getAlarmDetailList2DataStr().split("\\&");
			List<HashMap<String,Object>> tempList=new ArrayList<HashMap<String,Object>>();
			for (String track : tepmrows) {
				try
				{
					HashMap<String,Object> tpHash=new HashMap<String,Object>();
					String[] trackArray = track.split("\\#");
					for(int i=0;i<columnArrSpt.length;i++){
						if("undefined".equals(trackArray[i])){
							tpHash.put(columnArrSpt[i], "");
						}else{
							tpHash.put(columnArrSpt[i], trackArray[i]);
						}
						
					}
					tempList.add(tpHash);
				
				} catch (Exception e){
					e.printStackTrace();
				}
				
			}
			rowsList.add(tempList);
		}

		if(!"".equals(param.getAlarmTrackListName())){
			List<TrackPlayBack> result = trackPlayBackService.selectPlayBackTrack(
					param.getVehicleId(),
					param.getStartTime(),
					param.getEndTime(),
	    			-1,
	    			-1
	    	);
			
			HashMap<String, Object> tempHash=new HashMap<String, Object>();
			String titleArray =param.getAlarmTrackListTtiles();
			//"定位时间,纬度,经度,速度,里程,状态"
			String columnArray ="a,b,c,d,e,f,g";
			String widthArray ="120,120,120,120,120,120,120";
			tempHash.put("titleArray", titleArray.split(","));
			tempHash.put("columnArray", columnArray.split(","));
			tempHash.put("widthArray", widthArray.split(","));
			colModelList.add(tempHash);
			rowsNameList.add(param.getAlarmTrackListName());
			for(TrackPlayBack obj : result){
				HashMap<String, String> map = new HashMap<String, String>();
				map.put("a", param.getVehicleNo());
				map.put("b", obj.getGps_time());
				map.put("c", obj.getLatitude()+"");
				map.put("d", obj.getLongitude()+"");
				map.put("e", obj.getGps_speed()+"");
				map.put("f", obj.getGps_mileage()/10+"");
				map.put("g", obj.getStatus());
				rows.add(map);
			}
			
			rowsList.add(rows);
		}

		if(!"".equals(param.getTrackListtName())){
			List<TrackPlayBack> result = trackPlayBackService.selectPlayBackTrack(
					param.getVehicleId(),
					param.getStartTime(),
					param.getEndTime(),
	    			-1,
	    			-1
	    	);
			
			HashMap<String, Object> tempHash=new HashMap<String, Object>();
			String titleArray =param.getTrackListTtiles();
			//"定位时间,纬度,经度,速度,里程,状态"
			String columnArray ="a,b,c,d,e,f,g";
			String widthArray ="120,120,120,120,120,120,120";
			tempHash.put("titleArray", titleArray.split(","));
			tempHash.put("columnArray", columnArray.split(","));
			tempHash.put("widthArray", widthArray.split(","));
			colModelList.add(tempHash);
			rowsNameList.add(param.getTrackListtName());
			for(TrackPlayBack obj : result){
				HashMap<String, String> map = new HashMap<String, String>();
				map.put("a", param.getVehicleNo());
				map.put("b", obj.getGps_time());
				map.put("c", obj.getLatitude()+"");
				map.put("d", obj.getLongitude()+"");
				map.put("e", obj.getGps_speed()+"");
				map.put("f", obj.getGps_mileage()/10+"");
				map.put("g", obj.getStatus());
				rows.add(map);
			}
			
			rowsList.add(rows);
		}

		

		
		int index=-1;
		
		
		exportExlTrackPlay(request, response,param.getTitle(), colModelList,rowsList,imgsrcList,rowsNameList);
		
		
	}*/
}
